package com.yami.shop.listener;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yami.shop.bean.enums.DivideStatus;
import com.yami.shop.bean.enums.OrderType;
import com.yami.shop.bean.enums.PayStatus;
import com.yami.shop.bean.event.OrderSettlementEvent;
import com.yami.shop.bean.event.RefundSuccessSettlementEvent;
import com.yami.shop.bean.model.PayInfo;
import com.yami.shop.bean.order.ConfirmOrderOrder;
import com.yami.shop.bean.param.hnaPay.BaseTransForm;
import com.yami.shop.config.HnaPayConfig;
import com.yami.shop.service.PayInfoService;
import com.yami.shop.service.ShopWalletService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Objects;


/**
 * 确认收货的事件
 * @author yami
 */
@Slf4j
@Component("orderSettlementShopListener")
@AllArgsConstructor
public class OrderSettlementListener {

    private final ShopWalletService shopWalletService;

    private final HnaPayConfig hnaPayConfig;

    private final PayInfoService payInfoService;

    /**
     * 订单确认收货后过了维权期的订单结算操作
     * @param event
     */
    @EventListener(OrderSettlementEvent.class)
    @Order(ConfirmOrderOrder.DEFAULT)
    public void defaultSettlementOrderEvent(OrderSettlementEvent event) {
        for (com.yami.shop.bean.model.Order order : event.getOrder()) {
            //如果是积分订单无需操作
            if(Objects.equals(order.getOrderType() , OrderType.SCORE.value())){
                continue;
            }
            shopWalletService.addAmountBySettledOrder(order.getOrderNumber());
            // 支付信息
            PayInfo payInfo = payInfoService.getOne(new LambdaQueryWrapper<PayInfo>().eq(PayInfo::getPayStatus, PayStatus.PAYED.value()).eq(PayInfo::getOrderNumbers, order.getOrderNumber()));
            if(ObjectUtil.isNotEmpty(payInfo)){
                // 调用新生确认收货接口
                PayInfo newPayInfo = new PayInfo();
                newPayInfo.setPayNo(payInfo.getPayNo());
                try {
                    BaseTransForm base = hnaPayConfig.getF001TransForm(payInfo.getBizOrderNo(), payInfo.getDivideId(),null);
                    hnaPayConfig.post(base);
                    newPayInfo.setDivideStatus(DivideStatus.SUCCESS.value().toString());
                    payInfoService.update(newPayInfo, new LambdaUpdateWrapper<PayInfo>().eq(PayInfo::getPayNo, newPayInfo.getPayNo()));
                } catch (Exception e) {
                    newPayInfo.setDivideStatus(DivideStatus.FAIL.value().toString());
                    payInfoService.update(newPayInfo, new LambdaUpdateWrapper<PayInfo>().eq(PayInfo::getPayNo, newPayInfo.getPayNo()));
                }
            }
        }

    }

    /**
     * 部分退款且订单关闭的情况下的订单结算操作
     * @param event
     */
    @EventListener(RefundSuccessSettlementEvent.class)
    @Order(ConfirmOrderOrder.DEFAULT)
    public void refundSuccessSettlementOrderEvent(RefundSuccessSettlementEvent event) {
        shopWalletService.settlementShopAmountByRefund(event.getOrderChangeShopWalletAmountBO());

    }

}
